//
//  InstallationImageListTopView.swift
//  ezyform
//
//  Created by MoennigQ on 18/10/21.
//  Copyright © 2021 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit

class ImagesListTopView: UIView {
    var originalDescrip: String = ""
    let originalDescripMaxHeight: CGFloat = 200
    let tipImageView = UIImageView()
    //description
    let descriptionTextView = BaseTextView()
    let miniImagesCntLabel = BaseLabel()
    let bottomLineView = View(hexStr: "#F5F5F5FF")
    var miniImagesCntLabelHeightConstraint: Constraint!
    var descriptionTextViewHeightConstraint: Constraint!
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUpView() {
        self.backgroundColor = .white
        //tip image
        self.addSubview(tipImageView)
        tipImageView.snp.makeConstraints { make in
            make.left.top.equalTo(16)
            make.width.height.equalTo(16)
        }
        //tip label
        descriptionTextView.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)//设置所有的编辑为0，正确获取最大的高度，底部有个10的高度
        descriptionTextView.backgroundColor = .white
        descriptionTextView.setFont(R.font.interMedium(size: 14))
        descriptionTextView.textColor = UIColor(hexStr: "#292D3299")
        descriptionTextView.isEditable = false
        self.addSubview(descriptionTextView)
        descriptionTextView.snp.makeConstraints { make in
            make.top.equalTo(13)
            make.right.equalTo(-15)
            make.left.equalTo(45)
            //这里必须是make.height.equalTo(0)，不能是greaterThanOrEqualTo(0)，否则布局会被拉伸，image list的table view显示不出来
            self.descriptionTextViewHeightConstraint = make.height.equalTo(0).constraint
        }
        //mini count label
        miniImagesCntLabel.setFont(R.font.interMedium(size: 15))
        miniImagesCntLabel.textColor = UIColor(hexStr: "#379657")
        self.addSubview(miniImagesCntLabel)
        miniImagesCntLabel.snp.makeConstraints { make in
            make.top.equalTo(descriptionTextView.snp.bottom)
            make.left.equalTo(16)
            make.width.greaterThanOrEqualTo(0)
            self.miniImagesCntLabelHeightConstraint = make.height.equalTo(0).constraint
        }
        //bottom line
        self.addSubview(bottomLineView)
        bottomLineView.snp.makeConstraints { make in
            make.top.equalTo(miniImagesCntLabel.snp.bottom)
            make.height.equalTo(4)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    func setData(_ checklistModel: PVDChecklistModel){
        let oldDescription: String = self.originalDescrip
        let minImagesCnt: Int64 = checklistModel.minimum_quantity
        
        self.originalDescrip = checklistModel.description
        
        self.descriptionTextView.text = self.originalDescrip
        //description
        if self.originalDescrip.count > 0{//description显示
            //
            self.tipImageView.image = R.image.installation_imagelist_top_tip()
            if oldDescription != self.originalDescrip{//不等于才更新
                //显示的最大字符数
                let heightForAll = self.heightForTextView(textView: self.descriptionTextView)
                if heightForAll > originalDescripMaxHeight{
                    self.descriptionTextViewHeightConstraint.update(offset: originalDescripMaxHeight)
                    self.descriptionTextView.showsVerticalScrollIndicator = true
                }else{
                    self.descriptionTextViewHeightConstraint.update(offset: heightForAll)
                    self.descriptionTextView.showsVerticalScrollIndicator = false
                }
            }
        }else{//description不显示
            self.tipImageView.image = nil
            self.descriptionTextView.text = ""
            self.descriptionTextViewHeightConstraint.update(offset: 0)
        }
        //min count
        if minImagesCnt > 0{//min count显示
            self.miniImagesCntLabelHeightConstraint.update(offset: 40)
            self.miniImagesCntLabel.text = "Minimum quantity: \(minImagesCnt)"
        }else{//min count不显示
            self.miniImagesCntLabel.text = ""
            self.miniImagesCntLabelHeightConstraint.update(offset: 0)
        }
    }
    func heightForTextView(textView: UITextView) -> CGFloat {
        let size = CGSize(width: APPConfig.WidthForPortrait - 92, height:  CGFloat(MAXFLOAT))
        let realSize = textView.sizeThatFits(size)
        return realSize.height
    }
}
